/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package edu.nii.qlks.BusinessLogicLayer;

import com.microsoft.sqlserver.jdbc.SQLServerDataSource;
import edu.nii.qlks.DataAccessLayer.MSSQLConnectUnit;
import edu.nii.qlks.DataTransferObject.ServerInfo;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.sql.Connection;

/**
 *
 * @author MARS_NHO
 */
public class BusinessLogicAbstract {
    //MSSQLConnectUnit _connect;
    
    private static ServerInfo serverInfo = new ServerInfo();
    
    //KIEM TRA KET NOI LOAD TU FILE CONFIGURE
    public static boolean checkConnection() {
        Connection cn = null;
        try {
            File dataf = new File("server.cfg");
            int number;
            int i = 0;

            //Create a reader for the file
            FileReader fdat = new FileReader(dataf);
            BufferedReader dat = new BufferedReader(fdat);
            System.out.println("DATA FROM THE FILE:");

            String tenDatabase = "";
            String tenServer = "";
            int port = 0;
            String tenUser = "";
            String pass = "";

            while (dat.ready()) {
                if (i == 0) {
                    tenDatabase = dat.readLine();
                }
                if (i == 1) {
                    tenServer = dat.readLine();
                }
                if (i == 2) {
                    int num = Integer.parseInt(dat.readLine());
                    port = num;
                }
                if (i == 3) {
                    tenUser = dat.readLine();
                }
                if (i == 4) {
                    pass = dat.readLine();
                }
                i++;
            }
            System.out.println("END OF FILE REACHED");
            dat.close();

            SQLServerDataSource ds = new SQLServerDataSource();
            ds.setUser(tenUser);
            ds.setPassword(pass);
            ds.setServerName(tenServer);
            ds.setPortNumber(port);
            ds.setDatabaseName(tenDatabase);
            
            cn = ds.getConnection();
            
            serverInfo.setDataBase(tenDatabase);
            serverInfo.setHost(tenServer);
            serverInfo.setPort(port);
            serverInfo.setUserName(tenUser);
            serverInfo.setPassword(pass);

        } catch (Exception e) {
            return false;
        }
        return cn != null;
    }
    
    public static MSSQLConnectUnit getConnection(){
        
        return new MSSQLConnectUnit(serverInfo.getHost(), serverInfo.getPort(), serverInfo.getUserName(), serverInfo.getPassword(), serverInfo.getDataBase());
    }
    
    public static String TaoMaTuDong(String maGoc, int batDau){
        String str, kq = "";
        String _maGoc = maGoc.trim();
        int so;
        try{
            str = _maGoc.substring(batDau);
            kq = _maGoc.substring(0, batDau);
            so = Integer.parseInt(str) + 1;
            //String temp = String.valueOf(so);
            for (int i = 0; i < (str.length() - String.valueOf(so).length()); i++){
                kq += "0";
            }
            kq = kq + String.valueOf(so);
            //return kq;
        }
        catch(Exception e){
            return "";
        }
        return kq;
    }
    
    public static Connection LayKetNoi() {
        Connection cn = null;
        try {
            File dataf = new File("server.cfg");
            int number;
            int i = 0;

            //Create a reader for the file
            FileReader fdat = new FileReader(dataf);
            BufferedReader dat = new BufferedReader(fdat);
            System.out.println("DATA FROM THE FILE:");

            String tenDatabase = "";
            String tenServer = "";
            int port = 0;
            String tenUser = "";
            String pass = "";

            while (dat.ready()) {
                if (i == 0) {
                    tenDatabase = dat.readLine();
                }
                if (i == 1) {
                    tenServer = dat.readLine();
                }
                if (i == 2) {
                    int num = Integer.parseInt(dat.readLine());
                    port = num;
                }
                if (i == 3) {
                    tenUser = dat.readLine();
                }
                if (i == 4) {
                    pass = dat.readLine();
                }
                i++;
            }
            System.out.println("END OF FILE REACHED");
            dat.close();

            SQLServerDataSource ds = new SQLServerDataSource();
            ds.setUser(tenUser);
            ds.setPassword(pass);
            ds.setServerName(tenServer);
            ds.setPortNumber(port);
            ds.setDatabaseName(tenDatabase);
            
            cn = ds.getConnection(); 

        } catch (Exception e) {
            return null;
        }
        return cn;
    }
}
